/*
 * Project Beknyou
 * Copyright (c) 2010-2011 Saint Paul College, All Rights Reserved
 * Redistributions in source code form must reproduce the above
 * Copyright and this condition.
 * The contents of this file are subject to the GNU General Public
 * License, Version 2 (the "License"); you may not use this file
 * except in compliance with the License. A copy of the License is
 * available at http://www.opensource.org/licenses/gpl-license.php.
 */
package com.benkyou.common;

import com.benkyou.client.GameClient;
import com.benkyou.client.network.Chatter;
import com.jme3.asset.AssetManager;
import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.benkyou.client.systems.AnimationSystem;
import com.benkyou.client.systems.AvatarControlSystem;
import com.benkyou.util.TextImageGenerator;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.control.CharacterControl;
import com.jme3.material.Material;
import com.jme3.material.RenderState.BlendMode;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.texture.Image.Format;
import com.jme3.texture.Texture.WrapMode;
import com.jme3.texture.Texture2D;
import java.util.ArrayList;

/**
 *
 * @author Austin Allman
 */
public class Player extends Node{
    private Avatar avatarInfo;
    private Node avatar;
    private AssetManager mgr;
    private String name;
    private AnimationSystem animSys;
    private CharacterControl character_phys;
    private CapsuleCollisionShape capsuleShape;
    private GameClient gameClient;
    private AvatarControlSystem avatarControlSystem;
    private boolean attached;
    private boolean physicsSetup;
    private Vector3f moveDirection;
    private String uuid;
    private int id;
    private boolean atachementUpdate;
   public static final Vector3f spawnLocation = new Vector3f(10.0f, 50.0f, 10.0f);
    private boolean markedForDeletion;
    private Vector3f currentPosition;
    private Chatter chatter;

    public Player(String name, int id, String uuid) {
        this.name = name;
        this.id = id;
        this.uuid = uuid;
    }

    public Player(GameClient gameClient, int avatarNumber, String name, String uuid) {
        this.gameClient = gameClient;
        atachementUpdate = true;
        mgr = gameClient.getAssetManager();
        this.name = name;
        id = avatarNumber;
        avatarInfo = new Avatar(avatarNumber -1);
        avatar = (Node) mgr.loadModel(avatarInfo.getModelLocation());
        avatar.setLocalScale(5f);
        avatar.center();
        avatar.setName(name);
        avatar.setLocalTranslation(0f, -5f, 0f);
        this.attachChild(avatar);
        this.setLocalTranslation(15f, 5f, 0f);
        //gameClient.getRoot().attachChild(avatar);
        setupPlayerAnim();
        avatarControlSystem = new AvatarControlSystem(gameClient, this);
        attached = false;
        physicsSetup = false;
        moveDirection = Vector3f.ZERO;
        this.uuid = uuid;
    }
    public void setChatter(Chatter chatter){
        this.chatter = chatter;
    }
    public Chatter getChatter(){
        return chatter;
    }

    public Player setAsMainPlayer() {
        avatarControlSystem.setAsMainPlayer();
        return this;
    }

    private void setupPlayerAnim() {
        animSys = new AnimationSystem(avatar);
        animSys.setupAnimations();
        //animSys.animate(AnimationSystem.anim.Stand);
    }

    public Vector3f getPosition() {
        return avatar.getWorldTranslation();
    }

    public Spatial getAvatar() {
        return avatar;
    }

    public String getName() {
        return name;
    }


    public Player setupCharacterPhysics() {
        capsuleShape = new CapsuleCollisionShape(3f, 4f);
        character_phys = new CharacterControl(capsuleShape,  0.05f);
        character_phys.setApplyPhysicsLocal(true);
        this.addControl(character_phys);
       // avatar.addControl(character_phys);
        
        return this;
    }

    public CharacterControl getCharacterPhysics() {
        return character_phys;
    }

    public String toString() {
        return "This players name is: " + name + " avatar id: "+ id+ " uuid:" + uuid;
    }

    public AnimationSystem getAnimationSystem() {
        return animSys;
    }

    public AvatarControlSystem getAvatarControlSystem() {
        return avatarControlSystem;
    }
     Geometry cube_translucent;
    /*public void test(){
          
    Box boxshape3 = new Box(Vector3f.ZERO, 3f,1f,3f);
   cube_translucent = new Geometry("translucent cube", boxshape3);
    Material mat_tt = new Material(gameClient.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
    Texture2D tex = new Texture2D(500,500,Format.RGB8);
    mat_tt.setTexture("ColorMap", tex);
    tex.setImage(new TextImageGenerator(name).getTextImage());
    
    mat_tt.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
    cube_translucent.setMaterial(mat_tt); 
    attachChild(cube_translucent); 
    cube_translucent.setLocalTranslation(10f, 10f, -30f);
    }*/
     
    public String getUUID(){
        return uuid;
    }
    public boolean isAttached(){
        return attached;
    }

    public void setAttached(boolean attached) {
        this.attached = attached;
    }
    public boolean isPhysicsSetup(){
        return physicsSetup;
    }
    public void setPhysicsSetup(boolean physicsSetup){
        this.physicsSetup = physicsSetup;
    }

    public Vector3f getWalkDirection() {
        return moveDirection;
    }
    public void setWalkDirection(float x, float y, float z){
        moveDirection = new Vector3f(x, y,z);
    }
    public Vector3f getCurrentPosition(){
        return currentPosition;
    }
    public void setCurrentPosition(float posX, float posY, float posZ){
        currentPosition = new Vector3f(posX, posY, posZ);
    }
    public int getID(){
        return id;
    }
    public boolean isAtachementUpdate(){
        return atachementUpdate;
    }
    public void setAtachementUpdate(Boolean bol){
        atachementUpdate = bol;
    }

    public void setMarkedForDeletion(boolean marked) {
        markedForDeletion = marked;
    }
    public boolean isMarkedForDeletion(){
        return markedForDeletion;
    }
}
